/**
 * @author : zzr
 * @description :
 * @date :
 */
public class Test {

    public static void main(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addLast(8);
        mySingleList1.addLast(15);
        mySingleList1.addLast(30);
        mySingleList1.addLast(45);
        mySingleList1.addLast(80);

        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(10);
        mySingleList2.addLast(19);
        mySingleList2.addLast(23);
        mySingleList2.addLast(90);

        MySingleList.ListNode nh = mergeTwoLists(mySingleList1.head, mySingleList2.head);
        mySingleList1.display(nh);
    }

    public static MySingleList.ListNode mergeTwoLists(MySingleList.ListNode headA, MySingleList.ListNode headB) {

        MySingleList.ListNode newHead = new MySingleList.ListNode(-1); //不具备有效数据
        MySingleList.ListNode tmpHead = newHead;

        while (headA != null && headB != null) {
            if (headA.val > headB.val) {
                tmpHead.next = headB;
                headB = headB.next;
            } else {
                tmpHead.next = headA;
                headA = headA.next;
            }
            tmpHead = tmpHead.next;
        }

        if (headA != null) {
            tmpHead.next = headA;
        }
        if (headB != null) {
            tmpHead.next = headB;
        }
        return newHead.next; //注意这里返回的是newHead的next，因为是newHead的next中存放合并后的链表的首节点
    }
}
